package com.xsy.android.compose.wandroid.ui.view

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.rounded.MoreVert
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 *@author: xiaosy
 *时间: 2024/7/11
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun TopAppBar(
    modifier: Modifier = Modifier,
    title: String,
    showBack: Boolean = true,
    click: (() -> Unit)? = null,
    showRight: Boolean = false,
    rightImg: ImageVector = Icons.Rounded.MoreVert,
    rightClick: (() -> Unit)? = null
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(48.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        if (showBack) {
            if (click != null) {
                IconButton(
                    onClick = click,
                    modifier = Modifier.wrapContentWidth(Alignment.Start)
                ) {
                    Icon(
                        imageVector = Icons.AutoMirrored.Rounded.ArrowBack,
                        contentDescription = "back"
                    )
                }
            }

        }
        Text(
            modifier = Modifier
                .weight(1f)
                .wrapContentWidth(Alignment.CenterHorizontally),
            text = title,
            style = MaterialTheme.typography.titleMedium,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )

        if (showRight) {
            if (rightClick != null) {
                IconButton(
                    onClick = rightClick,
                    modifier = Modifier.wrapContentWidth(Alignment.End)
                ) {
                    Icon(imageVector = rightImg, contentDescription = "more")
                }
            }
        }

    }


}

@Preview(showBackground = true)
@Composable
private fun PreviewTopAppBar() {
    TopAppBar(title = "测试", showBack = true, click = {}, showRight = true, rightClick = {})
}